package top.zhimingw.controller;

import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import top.zhimingw.pojo.Article;
import top.zhimingw.pojo.Kind;
import top.zhimingw.redis.ArticleKey;
import top.zhimingw.utils.CacheUtils;
import top.zhimingw.utils.ImageUtils;
import top.zhimingw.utils.LogUtils;

import java.util.List;

/**
 * @ClassName: KindController
 * @Description:
 * @Author:zhimingw
 * @Version 1.0
 * @since 2021/7/16 14:00
 */
@Controller
public class KindController extends BaseController{
    private Logger logger = LogUtils.getInstance(KindController.class);
    
    /**
    * @Description: 添加文集
    * @Param: [kind]
    * @return: java.lang.String
    * @Author: zhimingw
    * @Date: 2021/7/16
    */
    @PostMapping("/addKind")
    @ResponseBody
    public String addKind(Kind kind){
        List<Kind> kinds = kindService.queryAll(new Kind(kind.getName()));
        if(kinds!=null&&kinds.size()!=0){
            result.setCode(501);
            logger.warn("【失败】添加文件，该名称已经被占用");
            return JSONObject.toJSONString(result);
        }
        //为文集设置随机封面
        kind.setImg(ImageUtils.getRandomFace());
        if(kindService.insert(kind)){
            result.setCode(200);
            CacheUtils.cleanKindCache(redisService,kind.getId());
            logger.warn("【成功】添加文集");
        }else {
            result.setCode(500);
            logger.warn("【失败】添加文集");
        }
        return JSONObject.toJSONString(result);
    }
    
    /**
    * @Description: 更新文集
    * @Param: [kind]
    * @return: java.lang.String
    * @Author: zhimingw
    * @Date: 2021/7/16
    */
    @PostMapping("/updateKind")
    @ResponseBody
    public String updateKind(Kind kind){
        // 检查用户是不是没有更改任何的信息
        Kind old = kindService.queryById(kind.getId());
        if (kind.getName().equals(old.getName()) && kind.getIntroduce().equals(old.getIntroduce())){
            result.setCode(501);
            logger.warn("【失败】更新文集，没有修改任何信息");
            return JSONObject.toJSONString(result);
        }
        // 如果更新之后的文集名称已被占用
        List<Kind> kinds = kindService.queryAll(new Kind(kind.getName()));
        if ( !kind.getName().equals(old.getName()) && kinds!=null&&kinds.size()!= 0){
            result.setCode(502);
            logger.warn("【失败】更新文集，该名称已被占用");
            return JSONObject.toJSONString(result);
        }
        if (kindService.update(kind)) {
            result.setCode(200);
            cleanCacheByKindId(kind.getId());
            CacheUtils.cleanKindCache(redisService,kind.getId());
            logger.warn("【成功】更新文集");
        }else {
            result.setCode(500);
            logger.warn("【失败】更新文集");
        }
        return JSONObject.toJSONString(result);
    }

    /**
    * @Description: 删除文集
    * @Param: [id]
    * @return: java.lang.String
    * @Author: zhimingw
    * @Date: 2021/7/16
    */
    @GetMapping("/deleteKind")
    public String delteKind(Integer id){
        if(id!=null){
            if(kindService.deleteById(id)){
                cleanCacheByKindId(id);
                CacheUtils.cleanKindCache(redisService,id);
                logger.warn("【成功】删除文集");
            }else {
                logger.warn("【失败】删除文集");
            }
        }
        return "redirect:/kinds";
    }

    /**
    * @Description: 清理标签ID清理文章缓存
    * @Param: [kId]
    * @return: void
    * @Author: zhimingw
    * @Date: 2021/7/16
    */
    private void cleanCacheByKindId(Integer kId){
        List<Article> articles = articleService.findByKindId(kId);
        articles.forEach(article->{
            redisService.del(ArticleKey.getById,String.valueOf(article.getId()));
        });
        logger.warn("清理文集涉及的文章缓存，KindId："+kId);
    }
}
